//
// Created by 赵江明 on 16/8/11.
//

#import "HttpManager+In.h"

#import <ReactiveCocoa/ReactiveCocoa.h>

#import "HttpConstant.h"
#import "AFHTTPSessionManager+In.h"

@implementation HttpManager (In)

- (RACSignal *)getPicstatPath:(NSString *)path params:(NSDictionary *)params {
    return [self getDomain:BASE_PATH_PICSTAT path:path params:params customParams:nil];
}

- (RACSignal *)getStaticPath:(NSString *)path params:(NSDictionary *)params {
    return [self getDomain:BASE_PATH_STATIC path:path params:params customParams:nil];
}

- (RACSignal *)postStaticPath:(NSString *)path params:(NSDictionary *)params {
    return [self postDomain:BASE_PATH_STATIC path:path params:params customParams:nil];
}

- (RACSignal *)getUpdateInPath:(NSString *)path params:(NSDictionary *)params {
    return [self getDomain:UPDATE_PATH path:path params:params customParams:nil];
}

- (RACSignal *)getInPath:(NSString *)path params:(NSDictionary *)params {
    return [self getDomain:BASE_PATH path:path params:params customParams:nil];
}

- (RACSignal *)postInPath:(NSString *)path params:(NSDictionary *)params {
    return [self postDomain:BASE_PATH path:path params:params customParams:nil];
}

- (RACSignal *)getFeedbackPath:(NSString *)path params:(NSDictionary *)params {
    return [self getDomain:BASE_PATH_FEEDBACK path:path params:params customParams:nil];
}

- (RACSignal *)postFeedbackPath:(NSString *)path params:(NSDictionary *)params {
    return [self postDomain:BASE_PATH_FEEDBACK path:path params:params];
}

- (RACSignal *)postLogPath:(NSString *)path params:(NSDictionary *)params {
    return [self postDomain:BASE_PATH_LOG path:path params:params];
}

- (RACSignal *)getDomain:(NSString *)domain path:(NSString *)path params:(NSDictionary *)params customParams:(NSDictionary *)customParams {
    NSMutableDictionary *dictionary;
    if (params) {
        dictionary = [NSMutableDictionary dictionaryWithDictionary:params];

        [params enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *_Nonnull stop) {
            if ([obj isKindOfClass:[NSString class]] && [obj isEqual:@"(null)"]) {
                [dictionary removeObjectForKey:key];
            }
        }];

    } else {
        dictionary = [NSMutableDictionary dictionary];
    }

    NSMutableDictionary *header = [NSMutableDictionary new];
    if ([self token]) {
        [header setValue:[self token] forKey:@"token"];
    }
    if (customParams[@"UserAgent"]) {
        [header setValue:customParams[@"UserAgent"] forKey:@"UserAgent"];
    }

    if (domain) {
        [header setValue:domain forKey:@"host"];
    }

    return [[[[self rac_GET:[NSString stringWithFormat:@"http://%@%@", [self host2DNS:domain], path] parameters:dictionary headers:header customParams:customParams]
            map:^id(RACTuple *value) {
                return value;
            }] catch:^(NSError *error) {
        return [RACSignal error:error];
    }] replayLazily];
}

- (RACSignal *)postDomain:(NSString *)domain path:(NSString *)path params:(NSDictionary *)params {
    return [self postDomain:domain path:path params:params customParams:nil];
}

- (RACSignal *)postDomain:(NSString *)domain path:(NSString *)path params:(NSDictionary *)params customParams:(NSDictionary *)customParams {
    NSMutableDictionary *dictionary;
    if (params) {
        dictionary = [NSMutableDictionary dictionaryWithDictionary:params];
    } else {
        dictionary = [NSMutableDictionary dictionary];
    }

    NSMutableDictionary *header = [NSMutableDictionary new];
    if ([self token]) {
        [header setValue:[self token] forKey:@"token"];
    }

    if (domain) {
        [header setValue:domain forKey:@"host"];
    }

    return [[[[self rac_POST:[NSString stringWithFormat:@"http://%@%@", [self host2DNS:domain], path] parameters:dictionary headers:header customParams:customParams]
            map:^id(RACTuple *value) {
                return value;
            }] catch:^(NSError *error) {
        return [RACSignal error:error];
    }] replayLazily];
}

@end

@implementation HttpManager (RequestCustom)

- (RACSignal *)getInPath:(NSString *)path params:(NSDictionary *)params customParams:(NSDictionary *)customParams {
    return [self getDomain:BASE_PATH path:path params:params customParams:customParams];
}

- (RACSignal *)postInPath:(NSString *)path params:(NSDictionary *)params customParams:(NSDictionary *)customParams {
    return [self postDomain:BASE_PATH path:path params:params customParams:customParams];
}

@end
